package com.practice.util;


public class Trie {
	private Trie[] 	children;
	private boolean isWord;
	
	public Trie() {
		this.isWord = false;
		this.children = new Trie[26];
	}
	
	public void addWord(String word) {
		char[] c = word.toCharArray();
		Trie parent = this;
		for (int i=0; i<c.length; i++) {
			if (parent.getNode(c[i]) == null) {
				parent.addNode(c[i]);
			}
			parent = parent.getNode(c[i]);
		}
		parent.isWord = true;
	}
	
	public boolean isWord() {
		return this.isWord;
	}
	
	public void addNode(char c) {
		this.children[c - 'a'] = new Trie();
	}
	
	public Trie getNode(char c) {
		return this.children[c - 'a'];
	}
	
	public boolean hasNode(char c) {
		return getNode(c) != null;
	}
}
